package com.hqbzl.c1.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantReadWriteLock;

@Slf4j
public class TestReadLock {
    static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    static final ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
    static final ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();

    public static void main(String[] args) {

    }

    class Data {
        private Object data;

        public Object read() {
            readLock.lock();
            try {
                log.debug("read");
                return data;
            } finally {
                readLock.unlock();
            }
        }

        public void write(Object data) {
            writeLock.lock();
            try {
                log.debug("write");
                this.data = data;
            } finally {
                writeLock.unlock();
                log.debug("释放写锁");
            }
        }

    }
}
